package com.yang.studentsapp.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.yang.studentsapp.Applock.LockActivity;
import com.yang.studentsapp.R;
import com.yang.studentsapp.presenter.IViewRegister;
import com.yang.studentsapp.presenter.IViewSuccess;
import com.yang.studentsapp.presenter.SettingPresenterImpl;
import com.yang.studentsapp.ui.MyAppliction;
import com.yang.studentsapp.utils.WorkFactory;

public class RegisterActivity extends AppCompatActivity {
EditText editUserName,editPwd,editSecurity;
    SettingPresenterImpl presenter;
    TextView tvGologin;
    ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

    }

    private void initView() {
        presenter = new SettingPresenterImpl();
        editUserName = (EditText) findViewById(R.id.edit_user_name);
        editPwd = (EditText) findViewById(R.id.edit_password);
        editSecurity = (EditText) findViewById(R.id.edit_security);

        tvGologin = (TextView) findViewById(R.id.register_go_login);
        tvGologin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));

            }
        });
    }


    private String getText(EditText editText) {
        if (editText == null) {
            return null;
        }
        return editText.getText().toString().trim();
    }

    public void BtnLogin(View view) {
        final String name = getText(editUserName);
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        final String pwd = getText(editPwd);
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String security = getText(editSecurity);
        if (TextUtils.isEmpty(security)) {
            Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
//        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(pwd)) {
//            WorkFactory.instance().service().submit(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        EMClient.getInstance().createAccount(name, pwd);
//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                if (RegisterActivity.this.isFinishing()) {
//                                    dialog.dismiss();
//
//                                    MyAppliction.getInstance().setCurrentUserName(name);
//                                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registered_successfully), 0).show();
//                                    finish();
//                                }
//                            }
//                        });
//                    } catch (HyphenateException e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//        }
        presenter.register(name, pwd, new IViewRegister() {
            @Override
            public void onStart(int textId) {
                if (dialog == null) {
                    dialog = new ProgressDialog(RegisterActivity.this);
                    dialog.setMessage(getResources().getString(textId));
                    dialog.setCanceledOnTouchOutside(true);
                }
                dialog.show();
            }

            @Override
            public void onRegisterError(HyphenateException e) {
                dialog.dismiss();
                e.printStackTrace();
                Log.e("Tga", e.getDescription());
                int errorCode = e.getErrorCode();
                if (errorCode == EMError.NETWORK_ERROR) {
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
                } else if (errorCode == EMError.USER_ALREADY_EXIST) {
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.User_already_exists), Toast.LENGTH_SHORT).show();
                } else if (errorCode == EMError.USER_AUTHENTICATION_FAILED) {
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.registration_failed_without_permission), Toast.LENGTH_SHORT).show();
                } else if (errorCode == EMError.USER_ILLEGAL_ARGUMENT) {
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.illegal_user_name), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.Registration_failed) + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }, new IViewSuccess<String>() {
            @Override
            public void onSuccess(String data) {
                dialog.dismiss();
                Toast.makeText(RegisterActivity.this, "成功了！！！", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
            }
        });
    }
}
